/**
 * Copyright (c) 2008 Trent Hoeppner
 * 
 * This file is part of BillCalc.
 * 
 * BillCalc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BillCalc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BillCalc.  If not, see <http://www.gnu.org/licenses/>.
 **/

package com.trentech.billcalc.event;

import java.util.Date;

import testcases.MainTest;

import com.trentech.billcalc.FinanceManager;

/**
 * RenewLeaseEvent renews an existing Lease for a tenant.
 * 
 * @author Trent Hoeppner
 */
public class RenewLeaseEvent extends Event {
    
    /**
     * The new end of the lease.
     */
    private Date leaseEnd;
    

    /**
     * Constructor for RenewLeaseEvent.
     * 
     * @param   date        the date that the lease was renewed (should be the 
     *                      end of the old lease).  Cannot be null.
     * @param   name        the name of the tenant renewing the lease.  Cannot 
     *                      be null or empty.
     * @param   leaseEnd    the new end of the lease.  Cannot be null.
     */
    public RenewLeaseEvent(Date date, String name, Date leaseEnd) {
        super(date, name);
        
        this.leaseEnd = leaseEnd;
    }


    /**
     * {@inheritDoc}
     */
    public int compareTo(Event other) {
        int value = super.compareTo(other);
        if (value != 0) {
            return value;
        }
        
        RenewLeaseEvent otherCasted = (RenewLeaseEvent) other;
        value = compareLongs(leaseEnd.getTime(), otherCasted.getLeaseEnd().getTime());
        if (value != 0) {
            return value;
        }
        
        return 0;
    }
    
    
    /**
     * {@inheritDoc}
     */
    public void process(FinanceManager manager) {
        manager.renewLease(getDate(), getName(), leaseEnd);
    }
    
    
    /**
     * Returns the new end of the lease.
     * 
     * @return  the new end of the lease.  Will not be null.
     */
    public Date getLeaseEnd() {
        return leaseEnd;
    }
    
    
    /**
     * Sets the new end of the lease.
     * 
     * @param   leaseEnd    the new end of the lease.  Cannot be null.
     */
    public void setLeaseEnd(Date leaseEnd) {
        this.leaseEnd = leaseEnd;
    }


    /**
     * {@inheritDoc}
     */
    public void validate(String propertyName, FinanceManager manager) 
        throws InvalidResultException {
        
        super.validate(propertyName, manager);
        if (propertyName.equals("name")) {
            resident(propertyName, getName(), manager);
        }
        else if (propertyName.equals("leaseEnd")) {
            instanceOfDate(propertyName, getLeaseEnd());
            
            // original : 
            // if (!leaseEnd.after(getDate())) {
            // seed fault : 
            if (MainTest.isOriginal   ? (!leaseEnd.after(getDate())) : (!leaseEnd.after(getDate()) || true)) {
                throw new InvalidResultException(this, propertyName, "Must be after renewal date.");
            }
        }
    }
}
